﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FYP14
{
    public partial class AddUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    SetValues(Convert.ToInt32(Request.QueryString["ID"]));
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            User oUser = new User();
            oUser.login_id = txtUserID.Text;
            oUser.password = txtPassword.Text;
            oUser.user_role_id = Convert.ToInt32(ddlRoleID.SelectedValue);

            if (Request.QueryString["ID"] != null)
            {
                oUser.application_user_id = Convert.ToInt32(Request.QueryString["ID"]);
                BAL.UpdateUser(oUser);
            }
            else
            {
                BAL.SaveUser(oUser);
            }

            Response.Redirect("Users.aspx");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Users.aspx");
        }

        private void SetValues(Int32 ID)
        {
            DataTable dt = BAL.GetUser(ID);
            if (dt.Rows.Count > 0)
            {
                txtUserID.Text = dt.Rows[0]["login_id"].ToString();
                //txtPassword.Text = dt.Rows[0]["password"].ToString();
                txtPassword.Attributes.Add("value", dt.Rows[0]["password"].ToString());
                ddlRoleID.Items[ddlRoleID.Items.IndexOf(ddlRoleID.Items.FindByValue(dt.Rows[0]["user_role_id"].ToString()))].Selected = true;
            }
        }
    }
}